home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 2 Matrix Algebra / XMMATRIX / xmmatrix.cpp next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  1.8 KB  |  67 lines

  1. #include <windows.h> // for XMVerifyCPUSupport
  2. #include <DirectXMath.h>
  3. #include <DirectXPackedVector.h>
  4. #include <iostream>
  5. using namespace std;
  6. using namespace DirectX;
  7. using namespace DirectX::PackedVector;
  8.  
  9. // Overload the  "<<" operators so that we can use cout to 
  10. // output XMVECTOR and XMMATRIX objects.
  11. ostream& XM_CALLCONV operator << (ostream& os, FXMVECTOR v)
  12. {
  13.     XMFLOAT4 dest;
  14.     XMStoreFloat4(&dest, v);
  15.  
  16.     os << "(" << dest.x << ", " << dest.y << ", " << dest.z << ", " << dest.w << ")";
  17.     return os;
  18. }
  19.  
  20. ostream& XM_CALLCONV operator << (ostream& os, FXMMATRIX m)
  21. {
  22.     for (int i = 0; i < 4; ++i)
  23.     {
  24.         os << XMVectorGetX(m.r[i]) << "\t";
  25.         os << XMVectorGetY(m.r[i]) << "\t";
  26.         os << XMVectorGetZ(m.r[i]) << "\t";
  27.         os << XMVectorGetW(m.r[i]);
  28.         os << endl;
  29.     }
  30.     return os;
  31. }
  32.  
  33. int main()
  34. {
  35.     // Check support for SSE2 (Pentium4, AMD K8, and above).
  36.     if (!XMVerifyCPUSupport())
  37.     {
  38.         cout << "directx math not supported" << endl;
  39.         return 0;
  40.     }
  41.  
  42.     XMMATRIX A(1.0f, 0.0f, 0.0f, 0.0f,
  43.         0.0f, 2.0f, 0.0f, 0.0f,
  44.         0.0f, 0.0f, 4.0f, 0.0f,
  45.         1.0f, 2.0f, 3.0f, 1.0f);
  46.  
  47.     XMMATRIX B = XMMatrixIdentity();
  48.  
  49.     XMMATRIX C = A * B;
  50.  
  51.     XMMATRIX D = XMMatrixTranspose(A);
  52.  
  53.     XMVECTOR det = XMMatrixDeterminant(A);
  54.     XMMATRIX E = XMMatrixInverse(&det, A);
  55.  
  56.     XMMATRIX F = A * E;
  57.  
  58.     cout << "A = " << endl << A << endl;
  59.     cout << "B = " << endl << B << endl;
  60.     cout << "C = A*B = " << endl << C << endl;
  61.     cout << "D = transpose(A) = " << endl << D << endl;
  62.     cout << "det = determinant(A) = " << det << endl << endl;
  63.     cout << "E = inverse(A) = " << endl << E << endl;
  64.     cout << "F = A*E = " << endl << F << endl;
  65.  
  66.     return 0;
  67. }